const Router = require('@koa/router');
const userController = require('../../controllers/userController');
const { authenticate } = require('../../middleware/authMiddleware');
const { checkPermission } = require('../../middleware/permissionMiddleware');

const router = new Router({
    prefix: '/api/users'
});

/**
 * @swagger
 * /api/users/login:
 *   post:
 *     summary: 用户登录
 *     tags: [Users]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - username
 *               - password
 *               - captcha
 *             properties:
 *               username:
 *                 type: string
 *                 description: 用户名
 *               password:
 *                 type: string
 *                 description: 密码
 *               captcha:
 *                 type: string
 *                 description: 验证码
 *     responses:
 *       200:
 *         description: 登录成功
 */
router.post('/login', userController.login);

/**
 * @swagger
 * /api/users/register:
 *   post:
 *     summary: 用户注册
 *     tags: [Users]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - username
 *               - password
 *               - name
 *               - captcha
 *             properties:
 *               username:
 *                 type: string
 *               password:
 *                 type: string
 *               name:
 *                 type: string
 *               captcha:
 *                 type: string
 */
router.post('/register', userController.register);

/**
 * @swagger
 * /api/users/current:
 *   get:
 *     summary: 获取当前用户信息和路由
 *     tags: [Users]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取用户信息和路由
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: number
 *                 message:
 *                   type: string
 *                 data:
 *                   type: object
 *                   properties:
 *                     user:
 *                       type: object
 *                       properties:
 *                         id:
 *                           type: string
 *                         username:
 *                           type: string
 *                         nickName:
 *                           type: string
 *                         role:
 *                           type: string
 *                         avatar:
 *                           type: string
 *                         introduction:
 *                           type: string
 *                     menus:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           id:
 *                             type: string
 *                           title:
 *                             type: string
 *                           icon:
 *                             type: string
 *                           children:
 *                             type: array
 */
router.get('/current', authenticate, userController.getCurrentUser);

/**
 * @swagger
 * /api/users/captcha:
 *   get:
 *     summary: 获取验证码
 *     tags: [Users]
 *     responses:
 *       200:
 *         description: 成功获取验证码
 */
router.get('/captcha', userController.getCaptcha);

/**
 * @swagger
 * /api/users:
 *   get:
 *     summary: 获取用户列表
 *     tags: [Users]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页显示数量
 *       - in: query
 *         name: username
 *         schema:
 *           type: string
 *         description: 用户名或昵称搜索关键词
 *     responses:
 *       200:
 *         description: 成功获取用户列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: number
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 操作成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     total:
 *                       type: number
 *                     page:
 *                       type: number
 *                     limit:
 *                       type: number
 *                     data:
 *                       type: array
 *                       items:
 *                         type: object
 *                         properties:
 *                           id:
 *                             type: string
 *                           username:
 *                             type: string
 *                           nickName:
 *                             type: string
 *                           role:
 *                             type: string
 *                           avatar:
 *                             type: string
 *                           introduction:
 *                             type: string
 */
router.get('/',
    authenticate,
    checkPermission('system:user:list'),
    userController.getUsers
);

/**
 * @swagger
 * /api/users/{userId}/role:
 *   put:
 *     summary: 更改用户角色
 *     tags: [Users]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: userId
 *         required: true
 *         schema:
 *           type: string
 *         description: 用户ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - role
 *             properties:
 *               role:
 *                 type: string
 *                 description: 角色ID
 *           example:
 *             role: "role-id-123"
 *     responses:
 *       200:
 *         description: 用户角色更新成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 code:
 *                   type: number
 *                   example: 200
 *                 message:
 *                   type: string
 *                   example: 用户角色更新成功
 *                 data:
 *                   type: object
 *                   properties:
 *                     id:
 *                       type: string
 *                     username:
 *                       type: string
 *                     nickName:
 *                       type: string
 *                     role:
 *                       type: string
 *                     avatar:
 *                       type: string
 *       400:
 *         description: 无效的角色ID
 *       404:
 *         description: 用户不存在
 *       500:
 *         description: 服务器错误
 */
router.put('/:userId/role', authenticate, checkPermission('system:user:per'), userController.updateUserRole);

/**
 * @swagger
 * /api/users:
 *   post:
 *     summary: 添加用户（需要权限）
 *     tags: [Users]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - username
 *               - password
 *             properties:
 *               username:
 *                 type: string
 *                 description: 用户名
 *               password:
 *                 type: string
 *                 description: 密码
 *               nickName:
 *                 type: string
 *                 description: 昵称
 *               role:
 *                 type: string
 *                 description: 角色
 *                 default: user
 *               introduction:
 *                 type: string
 *                 description: 简介
 *               avatar:
 *                 type: string
 *                 description: 头像URL
 *           example:
 *             username: "newuser"
 *             password: "password123"
 *             nickName: "New User"
 *             role: "user"
 *     responses:
 *       201:
 *         description: 用户创建成功
 *       400:
 *         description: 请求参数错误
 *       403:
 *         description: 没有操作权限
 *       500:
 *         description: 服务器错误
 */
router.post('/',
    authenticate,
    checkPermission('system:user:add'),
    userController.createUser
);

/**
 * @swagger
 * /api/users/{id}:
 *   delete:
 *     summary: 删除用户
 *     tags: [Users]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 用户ID
 *     responses:
 *       200:
 *         description: 用户删除成功
 *       400:
 *         description: 无效的请求
 *       403:
 *         description: 没有操作权限
 *       500:
 *         description: 服务器错误
 */
router.delete('/:id',
    authenticate,
    checkPermission('system:user:del'),
    userController.deleteUser
);

/**
 * @swagger
 * /api/users/{id}:
 *   put:
 *     summary: 编辑用户信息
 *     tags: [Users]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 用户ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               username:
 *                 type: string
 *                 description: 用户名
 *               password:
 *                 type: string
 *                 description: 密码（可选）
 *               nickName:
 *                 type: string
 *                 description: 昵称
 *               avatar:
 *                 type: string
 *                 description: 头像URL
 *               introduction:
 *                 type: string
 *                 description: 个人简介
 *           example:
 *             username: "newusername"
 *             nickName: "新昵称"
 *             introduction: "这是我的简介"
 *     responses:
 *       200:
 *         description: 用户信息更新成功
 *       400:
 *         description: 请求参数错误
 *       404:
 *         description: 用户不存在
 *       500:
 *         description: 服务器错误
 */
router.put('/:id', authenticate, checkPermission('system:user:edit'), userController.updateUserInfo);

module.exports = router; 